﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMLTouch.Core.ViewModel;
using System.ComponentModel;
using System.Windows.Data;

namespace UMLTouch.UI.Start.ViewModels
{
    public class DiagramSelectionViewModel : PopupViewModel
    {
        String _pluginPath;

        DomainRecognitionCollection availableDiagrams;

        String _newDiagramName =String.Empty;

        public String NewDiagramName
        {
            get { return _newDiagramName; }
            set { 
                _newDiagramName = value;
                RaisePropertyChanged("NewDiagramName");
            }
        }

        public DomainRecognitionCollection AvailableDiagrams
        {
            get { return availableDiagrams; }
            protected set
            {
                availableDiagrams = value;
                RaisePropertyChanged("AvailableDiagrams");
            }
        }

        private void LoadPlugins()
        {
            availableDiagrams = DomainRecognitionCollection.Load(_pluginPath);
        }

        public DiagramSelectionViewModel(String PluginPath)
        {
            _pluginPath = PluginPath;
            LoadPlugins();
        }

        public DiagramSelectionViewModel()
            : base()
        {

        }

        public DomainRecognitionViewModel GetSelectedDiagram()
        {
            DomainRecognitionViewModel ret = null;
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.AvailableDiagrams);
            if (collectionView != null)
                ret = collectionView.CurrentItem as DomainRecognitionViewModel;

            return ret;
        }

        public DiagramContextViewModel CreateCurrentDiagramContext()
        {
            var dgFactory = GetSelectedDiagram();
            if (dgFactory != null)
            {
                String diagramContext = NewDiagramName.Equals(String.Empty) ? dgFactory.DefaultContextName : NewDiagramName;
                var ctxt = dgFactory.CreateContextViewModel(diagramContext);
                return ctxt;
            }
            return null;
        }

    }
}
